package cn.com.bluemoon.daps.model.utils.factory.impl.field;

import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import cn.com.bluemoon.daps.model.entity.DapDataModelTableField;
import cn.com.bluemoon.daps.model.utils.SqlUtils;
import cn.com.bluemoon.daps.model.utils.contrast.Constant;
import cn.com.bluemoon.daps.model.utils.factory.impl.table.HiveTableScript;
import cn.hutool.db.sql.SqlUtil;
import com.alibaba.druid.DbType;
import com.google.common.collect.Lists;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.lang.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author karry
 * @Data 2021/9/3
 * @Time:14:45
 */
public class HiveFieldScript extends AbsFieldScript {

    @Override
    public String createFieldScript(List<DapDataModelTableField> fields) {

        List<String> scriptList = Lists.newArrayList();
        fields.forEach(field -> {
            String haveDecimal = SqlUtils.isNull(field.getDecimalLength()) ? "" : "," + field.getDecimalLength();
            String length = String.format("(%s%s)", field.getLength(), haveDecimal);
            String dataLength = StringUtils.isBlank(field.getLength()) ? "" : length;
            String compoundOrLength = SqlUtils.checkIsCompoundType(field.getDataType()) ? field.getCompoundType() : dataLength;
            String script = String.format("`%s` %s%s", field.getFieldName(), field.getDataType(), compoundOrLength);
            scriptList.add(script);
        });
        String fieldScript = scriptList.stream().collect(Collectors.joining(","));
        return fieldScript;
    }

    @Override
    public void addFieldScript(List<DapDataModelTableField> fields, String tableSchema,
                               String tableName, Map<String, String> map) {
        String schema = StringUtils.isNotBlank(tableSchema) ? tableSchema + "." : "";
        String script = createFieldScript(fields);
        String fieldScript = String.format("%s%s%s add (%s);", Constant.AT, schema, tableName, script);
        map.put(Constant.CF, fieldScript);
    }

    @Override
    public String modifyFieldScript(DapDataModelTableField nf, DapDataModelTableField of, String tableName, String tableSchema) {

        String schema = StringUtils.isNotBlank(tableSchema) ? tableSchema + "." : "";
        String alter = String.format("%s%s%s", Constant.AT, schema, tableName);
        Boolean flag = SqlUtils.checkIsCompoundType(nf.getDataType());  // 是否为复合类型
        // 字段名、字段类型、长度、小数点、复合类型
        if(!nf.equals(of, DbType.hive)){
            String format = "%s change %s %s %s;";
            String length = StringUtils.isBlank(nf.getLength()) ? "" : String.format("(%s%s) ",nf.getLength(),
                    SqlUtils.isNull(nf.getDecimalLength()) ? "" : "," + nf.getDecimalLength());
            String dataType = String.format("%s%s", nf.getDataType(), flag ?  nf.getCompoundType() : length);
            String datatypeScript = String.format(format, alter, of.getFieldName(), nf.getFieldName(), dataType);
            return datatypeScript;
        }
        return "";
    }

    @Override
    public void deleteFieldScript(List<DapDataModelTableField> fields,
                                    String tableSchema, String tableName,
                                    Map<String, String> map) {

        String schema = StringUtils.isNotBlank(tableSchema) ? tableSchema + "." : "";
        String result = fields.stream().map(DapDataModelTableField::getFieldName)
                .collect(Collectors.joining(", ", "(", ")"));
        String fieldScript = String.format("%s%s%s replace columns %s;", Constant.AT, schema, tableName, result);
        map.put(Constant.DF, fieldScript);
    }
}
